//
//  SHorizontalView.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/10/29.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "SHorizontalView.h"
#import "SVerticalView.h"
#import "ICarouselView.h"
#import "ChatViewController.h"
#import "SessonContentMessage.h"
#import "AppDelegate.h"
#import "MainChatController.h"
#import "TouchTableView.h"

#define      KTableViewHeight       kScreenHeight - 70

@interface SHorizontalView()

@property(nonatomic,strong) ChatViewController *chatViewController;

@end

@implementation SHorizontalView

- (void)dealloc {
    
}

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        myPV = [[XKPagingView alloc] initVerticalWithFrame:frame];
        myPV.delegate = self;
        [self addSubview:myPV];
    }
    return self;
}

-(void)setDs:(NSArray *)aDS{
    
    if (_ds != aDS) {
        
        _ds = aDS;
        
    }
}

- (NSIndexPath *)getCurIndexPath {
    
    ICarouselView *svv = (ICarouselView *)[myPV viewForPageAtIndex:myPV.currentPageIndex];
    
    return [NSIndexPath indexPathForRow:svv.carousel.currentItemIndex inSection:myPV.currentPageIndex];
}

-(void)setCurIndexPath:(NSIndexPath *)aCurIndexPath{

    if ([self getCurIndexPath] != aCurIndexPath) {
        _curIndexPath = aCurIndexPath;
        
        myPV.currentPageIndex = aCurIndexPath.section;
        
        [self performSelector:@selector(delayOper) withObject:nil afterDelay:0.1];
    }
}

-(void)setCurIndexPath:(NSIndexPath *)aCurIndexPath openAnimator:(BOOL)openAnimator {
    
    if ([self getCurIndexPath] != aCurIndexPath) {
        
        _curIndexPath = aCurIndexPath;
        _openAnimator = openAnimator;
        [myPV setCurrentPageIndex:aCurIndexPath.section openAnimator:openAnimator];

        ICarouselView* svv = (ICarouselView*)[myPV viewForPageAtIndex:myPV.currentPageIndex];
        [svv.carousel setCurrentItemIndex:aCurIndexPath.row];
        
        ChatViewController *chatView = (ChatViewController *)[svv.carousel currentItemView];
        chatView.tableView.frame = CGRectMake(0, 20, kScreenWidth, KTableViewHeight);
        
        if (chatView && chatView.dataListOne && chatView.dataListOne.count > 0) {
            
            if (_isScrollToBottom) {
                [chatView scrollToBottom];
            } else {
                [chatView.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            }
        }
    } else {
        
        //收到机器人的消息滚动到最下面
        ICarouselView* svv = (ICarouselView*)[myPV viewForPageAtIndex:myPV.currentPageIndex];
        ChatViewController *chatView = (ChatViewController *)[svv.carousel currentItemView];
        if (chatView && openAnimator && _isScrollToBottom) {
            [chatView scrollToBottom];
        }
    }
}

#pragma mark - private methord

-(void)delayOper{
    
    ICarouselView* svv = (ICarouselView*)[myPV viewForPageAtIndex:myPV.currentPageIndex];
    [svv.carousel setCurrentItemIndex:_curIndexPath.row];
    
}

- (void)reloadData {
    
    [myPV reloadData];
    
}

- (void)reloadDataAtIndex:(NSUInteger)index {
    
    ICarouselView* svv = (ICarouselView*)[myPV viewForPageAtIndex:index];
    if (svv) {

        NSMutableArray *previousList = nil;
        NSMutableArray *nextList = nil;
        
        NSInteger previousIndex = index - 1;
        NSInteger nextIndex = index + 1;
        
        if (previousIndex >= 0) {
            previousList = [self.ds objectAtIndex:previousIndex];
        }
        if (nextIndex < self.ds.count) {
            nextList = [self.ds objectAtIndex:nextIndex];
        }
        
        svv.ds = [self.ds objectAtIndex:index];
        svv.previousDataList = previousList;
        svv.nextDataList = nextList;
        [svv.carousel reloadData];
        
    }
}

- (void)reloadDataAtIndexPath:(NSIndexPath *)indexPath {
    
    ICarouselView* svv = (ICarouselView*)[myPV viewForPageAtIndex:indexPath.section];
    if (svv) {
        
        NSMutableArray *previousList = nil;
        NSMutableArray *nextList = nil;
        
        NSInteger previousIndex = indexPath.section - 1;
        NSInteger nextIndex = indexPath.section + 1;
        
        if (previousIndex >= 0) {
            previousList = [self.ds objectAtIndex:previousIndex];
        }
        if (nextIndex < self.ds.count) {
            nextList = [self.ds objectAtIndex:nextIndex];
        }
        
        svv.ds = [self.ds objectAtIndex:indexPath.section];
        svv.previousDataList = previousList;
        svv.nextDataList = nextList;
        [svv.carousel reloadItemAtIndex:indexPath.row animated:YES];
    }
}

#pragma mark- ATPagingViewDelegate

- (NSInteger)numberOfPagesInPagingView:(XKPagingView *)pagingView{
    
//    NSMutableArray *lastList = self.ds.lastObject;
    
//    if (lastList.count == 1 && [(NSMutableArray *)lastList[0] count] == 1) {
//        
//        SessonContentMessage *message = (SessonContentMessage *)lastList[0][0];
//        if ([message.sys_code isEqualToString:@"9999"] || [message.sys_code isEqualToString:@"1005"]) {
//            return self.ds.count;
//        }
//    }

    return self.ds.count;
    
}

- (UIView *)viewForPageInPagingView:(XKPagingView *)pagingView atIndex:(NSInteger)index{
    
    if (index < self.ds.count) {
        
        NSMutableArray *previousList = nil;
        NSMutableArray *nextList = nil;
        
        NSInteger previousIndex = index - 1;
        NSInteger nextIndex = index + 1;
        
        if (previousIndex >= 0) {
            previousList = [self.ds objectAtIndex:previousIndex];
        }
        if (nextIndex < self.ds.count) {
            nextList = [self.ds objectAtIndex:nextIndex];
        }

        ICarouselView* svv = (ICarouselView*)[pagingView viewForPageAtIndex:index];
        if (svv) {
            NSArray* dsChild =[self.ds objectAtIndex:index];
            svv.ds = dsChild;
        } else {
            svv =[[ICarouselView alloc] initWithFrame:pagingView.frame ds:[self.ds objectAtIndex:index]];
        }
        
        svv.previousDataList = previousList;
        svv.nextDataList = nextList;

        svv.subjectDidSelectedBlock = ^(NSInteger index) {
            
            [pagingView reloadData];
            [self reloadDataAtIndex:index];
            pagingView.scrollView.scrollEnabled = YES;
        };
        
        svv.handlePinchDidTouchedBlock = ^(void) {
            
            pagingView.scrollView.scrollEnabled = NO;
        };
                
        [svv.carousel reloadData];
        
        return svv;
    } else {
        
        ICarouselView *svv =[[ICarouselView alloc] initWithFrame:pagingView.frame ds:nil];

        return svv;
    }
}

- (void)takeSnapshot {
    
//    self.snapShopImageView.image = [Util snapshot:self.chatViewController];

}

- (void)currentPageDidChangeInPagingView:(XKPagingView *)pagingView {
    
//    [self reloadDataAtIndexPath:kAppDelegate.rootVC.curIndexPath];
    [self reloadDataAtIndex:pagingView.currentPageIndex];
    
}

- (void)pagesDidChangeInPagingView:(XKPagingView *)pagingView {
    

}

- (void)pagingViewWillBeginMoving:(XKPagingView *)pagingView {
    
}

- (void)pagingViewDidEndMoving:(XKPagingView *)pagingView {
    

}

@end
